package oa.base.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import oa.base.spring.ApplicationPropertiesHolder;

public class FileUtils {
	
	public static String UTF_8 ="UTF-8";
	public static String GB2312 ="GB2312";
	public static String GBK ="GBK";
	
	public static String PATH="/upload/";
	
	public static String BASE_URL = ApplicationPropertiesHolder.getProperty("up.load.file.url");
	/**
	 * 保存文件
	 * 
	 * @param stream
	 * @return
	 */
	public void saveFile(InputStream stream, String path) {
		byte[] reads = new byte[4096];
		File file = new File(path);
		FileOutputStream outFile = null;
		BufferedInputStream inputStream = null;
		try {
			outFile = new FileOutputStream(file);
			inputStream = new BufferedInputStream(stream);
			int line = 0;
			while ((line = inputStream.read(reads)) != -1) {
				outFile.write(reads, 0, line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != inputStream) {
					inputStream.close();
				}
				if (null != outFile) {
					outFile.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	/**
	 * 保存文件
	 * 
	 * @param stream
	 * @return
	 */
	public void saveFile(File out, String path) {
		byte[] reads = new byte[4096];
		File file = new File(path);
		FileOutputStream outFile = null;
		BufferedInputStream inputStream = null;
		try {
			outFile = new FileOutputStream(file);
			inputStream = new BufferedInputStream(new FileInputStream(out));
			int line = 0;
			while ((line = inputStream.read(reads)) != -1) {
				outFile.write(reads, 0, line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != inputStream) {
					inputStream.close();
				}
				if (null != outFile) {
					outFile.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}

	/**
	 * 读取流的数据
	 * 
	 * @param inputStream
	 * @return
	 */
	public String readStream(InputStream inputStream) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
		char buffer[] = new char[4096];
		int len;
		StringBuffer sb = new StringBuffer();
		try {
			while ((len = reader.read(buffer)) > 0) {
				sb.append(new String(buffer, 0, len));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	
	
	/**
	 * 递归 删除文件及文件夹下面的所有文件
	 * 
	 * @param file
	 * @return
	 */
	public static boolean deleteFiles(File file) {
		File[] files = file.listFiles();
		if (files == null || files.length < 1) {
			// System.out.println("没有找到这个文件或文件夹");
			return false;
		}
		for (File deleteFile : files) {
			// System.out.println(deleteFile.getPath());
			if (deleteFile.isDirectory()) {
				// 如果是文件夹，则递归删除下面的文件后再删除该文件夹
				if (!deleteFiles(deleteFile)) {
					// 如果失败则返回
					return false;
				}
			} else {
				if (!deleteFile.delete()) {
					// 如果失败则返回
					return false;
				}
			}
		}
		return file.delete();
	}
	/**
	 * 递归 删除文件及文件夹下面的所有文件
	 * 
	 * @param file
	 * @return
	 */
	public static void deleteFile(File file) {
		if(file.exists()){
			file.delete();
		}
	}
	
	
	
	/**
	 * 拷贝文件
	 * 
	 * @param source
	 *            源文件
	 * @param dest
	 *            目标文件
	 * @return
	 */
	public static boolean copyFile(String source, String dest) {
			File in = new File(source);
			File out = new File(dest);
			return copyFile(in, out);
		
	}
	/**
	 * 拷贝文件
	 * 
	 * @param source
	 *            源文件
	 * @param dest
	 *            目标文件
	 * @return
	 */
	public static boolean copyFile(File source, File dest) {
		try {
			FileInputStream inFile = new FileInputStream(source);
			FileOutputStream outFile = new FileOutputStream(dest);
			byte[] buffer = new byte[1024];
			int i = 0;
			while ((i = inFile.read(buffer)) != -1) {
				outFile.write(buffer, 0, i);
			}// end while
			inFile.close();
			outFile.close();
			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	
	
	/**
	 * 剔除文件中满足这个的数据
	 * @param file
	 * @param regx 
	 * @param replace
	 * @return boolean 是否替换过  false 没有， true 替换过
	 */
	public static boolean readReplaceLine(File file,String regx,String replace){
		boolean isreplace = false;
		try {
			FileReader fileReader = new FileReader(file);
			BufferedReader reader = new BufferedReader(fileReader);
			List<String> str = new ArrayList<String>();
			String msg = null;
			while((msg=reader.readLine())!=null){
				if(msg.indexOf(regx)==-1){
					str.add(msg);
				}else{
					if(replace!=null){
					  str.add(replace);
					}
					isreplace = true;
				}
			}
			reader.close();
			fileReader.close();
			file.createNewFile();
			//写新文件内容
			FileWrite.writeLine(file, str);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return isreplace;
	}
	
	
	/**
	 * 这个文件中是否有匹配的数据
	 * @param file
	 * @param regx 
	 * @return boolean 是否替换过  false 没有， true 匹配
	 */
	public static boolean searchRegx(File file,String regx){
		boolean isRegx = false;
		try {
			FileReader fileReader = new FileReader(file);
			BufferedReader reader = new BufferedReader(fileReader);
			String msg = null;
			while((msg=reader.readLine())!=null){
				if(msg.indexOf(regx)!=-1){
					//是否有匹配的
					isRegx = true;
					System.out.println(file.getAbsolutePath());
					break;
				}
			}
			reader.close();
			fileReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return isRegx;
	}
	
	public static String getLocalUrl(String url){
		//如果他拥有前缀
		if(url.startsWith(BASE_URL)){
			return url;
		}
		StringBuffer code = new StringBuffer();
		code.append(getLocalUrl()+"\\");
		code.append(url);
		return code.toString();
	}
	public static String getLocalUrl(){
		StringBuffer code = new StringBuffer();
		code.append(BASE_URL);
		code.append("\\upload");
		return code.toString();
	}
	/**
	 * 获取新的文件路径名称
	 * @param fileName
	 * @return
	 * lilang 
	 * 2013-6-24下午7:58:16
	 */
	public static String getNewPath(String fileName){
		String newName = GenericsUtils.nextRandId()+substractSuffix(fileName);
		return getLocalUrl(newName);
	}
	
	
	public static String substractSuffix(String file){
		return file.substring(file.lastIndexOf("."));
	}
	
	
}
